class MemoryRedis:
    """内存模拟Redis客户端，用于开发环境"""
    def __init__(self):
        self.data = {}
        self.expire_times = {}

    def get(self, key):
        """获取键值"""
        # 检查过期时间
        if key in self.expire_times and self.expire_times[key] < time.time():
            del self.data[key]
            del self.expire_times[key]
            return None
        return self.data.get(key)

    def set(self, key, value, ex=None):
        """设置键值，支持过期时间(ex秒)"""
        self.data[key] = value
        if ex:
            self.expire_times[key] = time.time() + ex
        return True

    def delete(self, key):
        """删除键"""
        if key in self.data:
            del self.data[key]
        if key in self.expire_times:
            del self.expire_times[key]
        return True

    def expire(self, key, seconds):
        """设置过期时间"""
        if key in self.data:
            self.expire_times[key] = time.time() + seconds
            return True
        return False

    def ping(self):
        """模拟ping命令"""
        return True

    def flushall(self):
        """清空所有数据"""
        self.data = {}
        self.expire_times = {}
        return True

# 添加常用Redis方法的模拟实现
    def hget(self, hash_key, key):
        """获取哈希表字段值"""
        hash_data = self.data.get(hash_key, {})
        return hash_data.get(key)

    def hset(self, hash_key, key, value):
        """设置哈希表字段值"""
        if hash_key not in self.data:
            self.data[hash_key] = {}
        self.data[hash_key][key] = value
        return True

    def hdel(self, hash_key, key):
        """删除哈希表字段"""
        if hash_key in self.data and key in self.data[hash_key]:
            del self.data[hash_key][key]
            # 如果哈希表为空，删除整个哈希表
            if not self.data[hash_key]:
                del self.data[hash_key]
            return True
        return False

    def keys(self, pattern='*'):
        """查找匹配模式的键"""
        import fnmatch
        return fnmatch.filter(self.data.keys(), pattern)

import time